// 先把这个文件定义成一个模块
define(['template','req','jquery'],(template,req) => {
    class Header{
        constructor(){
            this.count = 0
            this.init()         
        }
        init(){
            //加载heaher内容
            $('header').load('/html/header.html',() => {
                this.getNavData()
                this.getuserInfo()
                this.addEvent()//使logo_jm滚动
            })
            
        }
        addEvent(){
            //添加滚动事件
            // $('.logo_jm')
            $(window).scroll(function(){
                // console.log($(this).scrollTop())
                let  t= $(this).scrollTop()
                if(t>142){
                    $('#logo_jm').css({position:'fixed','z-index':999})
                   
                }else{

                    $('#logo_jm').css({position:'relative'})
                }
                
            })
        }
        // 获取导航栏数据
        getNavData(){
           
            req.getHeaderNav()
            .then(res => {
                // console.log(res)
                const { code , data:{list}} = res
                // 截取数据
                const arr = list.splice(1,10)
                if(code === 200){
                    this.renderNav(arr)
                }
            })
        }
        // 渲染导航栏
        renderNav(list){
            console.log(list)
            const html = template('navTemplate',{list})
            $('.top_list').html(html)
            this.changeCount()
        }
        //渲染购物车
        changeCount(){
            this.count = 0
            // 从 localStorage 取数据
            let prodList = localStorage.getItem('prodList')
            if(prodList){
                prodList = JSON.parse(prodList)
                prodList.forEach(item => {
                    this.count += item.checked ? item.count : 0
                })
            }
            $('.top_shop em').text(this.count)
        }


        //获取地址栏数据
        getuserInfo(){
             let id_name=window.location.search.split('=')
            //  console.log(id_name)
             let a=0,flag=false
             for(let i=0;i<id_name.length;i++){
                 if(id_name[i]=="?user"){
                        a=i
                        flag=true
                 }

             }
            if(flag){
                let info=id_name[a+1]
                if(info){
                    //渲染地址栏
                    $('.userinfo b').html('您好！'+info)
                    
                }
            }
           
        }

    }
    return new Header()
})